"use client";
import { createContext, useContext } from "react";
import { redirect } from "next/navigation";
import { Session } from "next-auth";
import { CustomUser } from "@/types/user";
interface IAuthContext {
  user: CustomUser | null;
  login: () => void;
  logout: () => void;
}
const context: IAuthContext = {
  user: null,
  login: () => {},
  logout: () => {},
};
const AuthContext = createContext<IAuthContext>(context);
export interface IAuthProvider {
  children: React.ReactNode;
  context: Session | null;
}
export default function AuthProvider({ children, context }: IAuthProvider) {
  const user = context?.user || null;
  const login = async () => {
    redirect("/auth/login");
  };

  const logout = async () => {
    // Implement your logout logic here
  };
  const value: IAuthContext = {
    user,
    login,
    logout,
  };
  return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
}

export const useAuth = () => useContext(AuthContext);
